/**
 * <p>Title: ServiceZP15.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张志芳
 * @version 1.0
 * <pre>Histroy:
 *          2012-10-8 上午07:50:47  张志芳  Create
 *</pre>
 */
package com.baosight.sczx.zp.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.QueryMap;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.iplat4j.security.bridge.SecurityBridge;
import com.baosight.iplat4j.security.bridge.SecurityBridgeFactory;
import com.baosight.platform.core.security.authorization.Authorization;
import com.baosight.platform.core.security.authorization.AuthorizationManager;
import com.baosight.platform.core.security.base.Filter;
import com.baosight.platform.core.security.base.GlobalConfig;
import com.baosight.platform.core.security.base.SecurityFactory;
import com.baosight.platform.core.security.base.SortCondition;
import com.baosight.platform.core.security.resourceset.IResourcesetManager;
import com.baosight.platform.core.security.resourceset.Resourceset;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.zp.domain.ZPResourceset;
import com.baosight.sczx.zp.domain.ZPUserset;

/**
 * 角色管理——资源组列表
 * <p>Title: ServiceZP15.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张志芳
 * @version 1.0
 * <pre>Histroy:
 *          2012-10-8 上午07:50:47  张志芳  Create
 *</pre>
 */
public class ServiceZP15 extends ServiceEPBase {

	/**
	 * 初始化
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZP15</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-15上午07:25:10 张志芳
	 * </p>
	 *
	 */
	public EiInfo initLoad(EiInfo inInfo){
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
	
	/**
	 * 查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZP15</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-15上午07:25:20 张志芳
	 * </p>
	 *
	 */
	public EiInfo query(EiInfo inInfo)
	{
		//获得角色主键
		String businessUnitPost = inInfo.getString("roleId");		
		if(businessUnitPost==null){
			inInfo.getBlock(EiConstant.resultBlock).set(EiConstant.offsetStr, 0);
			inInfo.getBlock(EiConstant.resultBlock).set(EiConstant.countStr, 0);
			return inInfo;
		}

		//构造返回值outInfo
		EiInfo outInfo = new EiInfo();
		outInfo.setBlock(inInfo.getBlock(EiConstant.queryBlock));
		outInfo.setAttr(inInfo.getAttr());
		EiBlock resultBlock = outInfo.addBlock(EiConstant.resultBlock);
		resultBlock.setBlockMeta(new ZPResourceset().eiMetadata);

		//提取查询条件
		QueryMap queryMap = QueryMap.getQueryMap(inInfo);
		
		//通过epass提供的类查询某角色下的资源组
		SecurityFactory f = SecurityFactory.getInstance();
		AuthorizationManager aManager = (AuthorizationManager) f.getAuthorizationManager();
		List auths = aManager.getAuthorizations(businessUnitPost, GlobalConfig.TYPE_USERSET, null, GlobalConfig.TYPE_RESOURCESET, SecurityBridge.OPT_ACCESS, queryMap.getOffset(), queryMap.getLimit(),null);
		List<ZPResourceset> resourcesets = new ArrayList<ZPResourceset>();
		for (int n = 0; n < auths.size(); n++) {
			Authorization auth = (Authorization) auths.get(n);
			ZPResourceset set = authToResourceset(auth);
			resourcesets.add(set);
		}
		resultBlock.setRows(resourcesets);
		
		int totalnum = aManager.getAuthorizationCount(businessUnitPost, GlobalConfig.TYPE_USERSET, null, GlobalConfig.TYPE_RESOURCESET, SecurityBridge.OPT_ACCESS);
		queryMap.setCount(totalnum);
		queryMap.setEiInfo(outInfo, EiConstant.resultBlock);
		

		return outInfo;
	}
	
	private ZPResourceset authToResourceset(Authorization auth) {
		IResourcesetManager rManager = SecurityFactory.getInstance().getResourcesetManager();
		Resourceset set = rManager.getResourceset(auth.getObjectName());

		ZPResourceset resset = new ZPResourceset();
		resset.setLabel(set.getName());
		resset.setName(set.getDisplayName());
		resset.setDesc(set.getDesc());

		return resset;

	}
	
	/**
	 * 添加资源组前的查询
	 * <p>Function: queryRes</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-15上午07:25:29 张志芳
	 * </p>
	 *
	 */
	public EiInfo queryRes(EiInfo inInfo)
	{
		//构造返回值outInfo
		EiInfo outInfo = new EiInfo();
		outInfo.setAttr(inInfo.getAttr());
		outInfo.setBlock(inInfo.getBlock(EiConstant.queryBlock));
		EiBlock rsetBlock = outInfo.addBlock("resResult");
		rsetBlock.setBlockMeta(new ZPResourceset().eiMetadata);
		

		
		//获取查询条件
		QueryMap queryMap = new QueryMap();
		queryMap.loadEiInfo(inInfo, EiConstant.queryBlock, "resResult");
		Filter filter = new Filter();
		if (queryMap.containsKey("Name")) {
			filter.put("Name", queryMap.get("Name"));
		}
		if (inInfo.getString("resgName") != null && !"".equals(inInfo.getString("resgName"))) {
			filter.put("Name", inInfo.getString("resgName"));
		}
		SortCondition sort = new SortCondition();//排序
		sort.addAsc("Name");
		
		//通过epass查询资源组
		IResourcesetManager rManager = SecurityFactory.getInstance().getResourcesetManager();
		List resourcesets = rManager.getResourcesets(filter, 0, 1000, sort);		
		
		//去除已经添加过的资源组
		String roleId = inInfo.getString("roleId");
		if(roleId != null){
			AuthorizationManager aManager = (AuthorizationManager) SecurityFactory.getInstance().getAuthorizationManager();
			List auths = aManager.getAuthorizations(roleId, GlobalConfig.TYPE_USERSET, null, GlobalConfig.TYPE_RESOURCESET, SecurityBridge.OPT_ACCESS, 0, -1, null);
			
			for (int n = 0; n < resourcesets.size(); n++) {
				Resourceset res = (Resourceset)resourcesets.get(n);
				Boolean flag = true;
				for(int m = 0; m < auths.size(); m++){
					Authorization auth = (Authorization) auths.get(m);
					Resourceset set = rManager.getResourceset(auth.getObjectName());
					if(res.getName().equals(set.getName())){
						flag = false;
						break;
					}
				}
				if(flag){
					rsetBlock.addRow(res);
				}				
			}
		}
		else{		
			rsetBlock.setRows(resourcesets);
		}

		return outInfo;
	}
	
	/**
	 * 添加资源组
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZP15</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-15上午07:26:49 张志芳
	 * </p>
	 *
	 */
	public EiInfo add(EiInfo inInfo)
	{
		String roleLabel = inInfo.getString("roleId");
		List resourcesets = inInfo.getBlock("result").getRows();
		for (int i = 0; i < resourcesets.size(); i++) {
			Map set = (Map) resourcesets.get(i);
			String setName = (String) set.get("Name");
			SecurityBridgeFactory.getBridge().grantAccess(roleLabel, setName, GlobalConfig.TYPE_RESOURCESET);
		}
		return inInfo;
	}
	
	/**
	 * 删除资源组
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZP15</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-15上午07:27:04 张志芳
	 * </p>
	 *
	 */
	public EiInfo delete(EiInfo inInfo)
	{
		String roleId = inInfo.getString("roleId");
		List resgs = inInfo.getBlock("result").getRows();
		for (int i = 0; i < resgs.size(); i++) {
			Map resg = (Map) resgs.get(i);
			String setName = (String) resg.get("Name");
			SecurityBridgeFactory.getBridge().revokeAccess(roleId, setName, GlobalConfig.TYPE_RESOURCESET);
		}
		return query(inInfo);
	}
	
	
	/**
	 * 查询需要删除资源组(ZP18中用到)
	 * <p>Function: queryDelRes</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZP15</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-15上午07:25:20 张志芳
	 * </p>
	 *
	 */
	public EiInfo queryDelRes(EiInfo inInfo)
	{
		//获得角色主键
		String businessUnitPost = inInfo.getString("roleId");		

		//构造返回值outInfo
		EiInfo outInfo = new EiInfo();
		outInfo.setBlock(inInfo.getBlock(EiConstant.queryBlock));
		outInfo.setAttr(inInfo.getAttr());
		EiBlock resultBlock = outInfo.addBlock("resResult");
		resultBlock.setBlockMeta(new ZPResourceset().eiMetadata);
		
		//通过epass提供的类查询某角色下的资源组
		SecurityFactory f = SecurityFactory.getInstance();
		AuthorizationManager aManager = (AuthorizationManager) f.getAuthorizationManager();
		List auths = aManager.getAuthorizations(businessUnitPost, GlobalConfig.TYPE_USERSET, null, GlobalConfig.TYPE_RESOURCESET, SecurityBridge.OPT_ACCESS, 0, 1000,null);
		List<ZPResourceset> resourcesets = new ArrayList<ZPResourceset>();
		for (int n = 0; n < auths.size(); n++) {
			Authorization auth = (Authorization) auths.get(n);
			ZPResourceset set = authToResourceset(auth);
			resourcesets.add(set);
		}
		resultBlock.setRows(resourcesets);
	
		return outInfo;
	}
}
